#' Get ISD data from NOAA
#'
#' This function downloads and cleans the NOAA Integrated Surface Data (ISD)
#' for the ISD station and year requested
#' 
#' @param year year for which data is needed
#' @param station ISD station for which data is needed
#' @param save_file True is source file should be saved
#' 


require(threadr)

getISD <- function(year, station, directory = ".", save_file = FALSE){
  
  output <- tryCatch(
    
    {
      # downloading data
      
      url <- paste("ftp://ftp.ncdc.noaa.gov/pub/data/noaa/isd-lite/", year, "/", station, "-", year, ".gz", sep = "")
      
      download_ftp_file(file_remote = url,
                        file_local = paste(directory, "/", station, "-", year, ".gz", sep = ""),
                        credentials = "anonymous:noaa")
      
      # creating dataset
      
      isd_data <- read_table(gzfile(paste(directory, "/", station, "-", year, ".gz", sep = "")),
                             col_names = c("year", "month", "day", "hour", "temp", "dew", "press", "wind_dir", "wind_speed", "sky", "prec_1", "prec_6"),
                             col_types = cols()) %>% 
        mutate(station = station,
               temp = ifelse(temp == -9999 | temp == 9999, NA, temp),
               dew = ifelse(dew == -9999 | dew == 9999, NA, dew),
               press = ifelse(press == -9999 | press == 9999, NA, press),
               wind_speed = ifelse(wind_speed == -9999 | wind_speed == 9999, NA, wind_speed),
               prec_1 = ifelse(prec_1 == -9999 | prec_1 == 9999, NA, prec_1),
               sky = ifelse(sky == -9999 | sky == 9999, NA, sky),
               prec_6 = ifelse(prec_6 == -9999 | prec_6 == 9999, NA, prec_6),
               temp = ((temp/10)*(9/5))+32,
               dew = ((dew/10)*(9/5))+32,
               press = press/10,
               wind_speed = wind_speed/10,
               prec_1 = prec_1/10,
               prec_6 = prec_6/10,
               prec_1 = ifelse(prec_1 == -0.1, 0.01, prec_1)) %>% 
        select(-c(wind_dir, prec_6))
      
      # removing NA indicators
      
      isd_data[isd_data == -9999 | isd_data == 9999] = NA
      
      # saving/deleting data file
      
      if (save_file){
        # do not delete file
      }else{
        unlink(paste(directory, "/", station, "-", year, ".gz", sep = ""))
      }
      
      # close file connections
      closeAllConnections()
      
      return(isd_data)
      
    },
    error = function(cond){
      
      if ("Could not resolve host: ftp.ncdc.noaa.gov" %in% cond){
        print(paste("Request timed out. Please re-try download.","Station:",station, " Year:", year))
      }else{
        print(paste("Data for ", station, " in ", year, " not available"))
      }
      return(NA)
    }
  )
  
  
  
  return(output)
}
